@namespace MudBlazor.UnitTests.TestComponents

<MudDataGrid T="Fruit" @ref="dataGrid" Items="@Fruits" Filterable="true" Hideable="true" Groupable="true"
    GroupExpanded="true" RowContextMenuClick="@OnRowContextMenuClick">
    <ToolBarContent>
        <MudText Typo="Typo.h6">Fruits</MudText>
        <MudSpacer />
    </ToolBarContent>
    <Columns>
        <PropertyColumn Property="x => x.Name" Title="Name" Filterable="false" Groupable="false" />
        <PropertyColumn Property="x => x.Count" />
        <PropertyColumn Property="x => x.Category" Title="Category" Grouping="true" GroupBy="@_groupBy">
            <GroupTemplate>
                <span style="font-weight:bold">Category: @context.Grouping.Key</span>
            </GroupTemplate>
        </PropertyColumn>
    </Columns>
</MudDataGrid>

<div class="d-flex flex-wrap mt-4">
    <MudButton Class="expand-all" OnClick="@ExpandAllGroups" Color="@Color.Primary">Expand All</MudButton>
    <MudButton OnClick="@CollapseAllGroups" Color="@Color.Primary">Collapse All</MudButton>
    <MudButton Class="add-button" OnClick="@AddFruit" Color="@Color.Primary">Add Fruit</MudButton>
</div>

@code {
    MudDataGrid<Fruit> dataGrid;

    public class Fruit
    {
        public string Name { get; set; }
        public int Count { get; set; }
        public string Category { get; set; }

        public Fruit(string name, int count, string category)
        {
            Name = name;
            Count = count;
            Category = category;
        }
    }
    public bool RowContextMenuClicked { get; set; }

    List<Fruit> Fruits { get; set; } = new()
    {
        new Fruit("Apple", 2, "Pome"),
        new Fruit("Pear", 4, "Pome"),
        new Fruit("Orange", 4, "Citrus")
    };
    
    Func<Fruit, object> _groupBy = x => 
    {
        return x.Category;
    };

    public void AddFruit()
    {
        Fruits.Add(new Fruit("Banana", 5, "Musa"));
    }

    public void ExpandAllGroups()
    {
        dataGrid?.ExpandAllGroups();
    }

    public void CollapseAllGroups()
    {
        dataGrid?.CollapseAllGroups();
    }

    private void OnRowContextMenuClick(DataGridRowClickEventArgs<Fruit> args)
    {
        RowContextMenuClicked = true;
    }
}